package com.leased.controller;

import com.leased.enums.BusinessStatusEnum;
import com.leased.exception.YCException;
import com.leased.handler.UserVerifyHandler;
import com.leased.pojo.UserVerify;
import com.leased.request.cuser.CUserListRequest;
import com.leased.response.UserVerifyResponse;
import com.leased.utils.APIResponse;
import com.leased.utils.Page;
import com.leased.utils.YCSystemStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by shencx on 2017/8/3.
 */
@Controller
@RequestMapping(value = "/api/verify")
public class UserVerifyController {

    final Logger LOG = LoggerFactory.getLogger(UserVerifyController.class);

    @Resource
    private UserVerifyHandler userVerifyHandler;

    /**
     * 获取车主认证列表
     * @param request
     * @param cUserListRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public APIResponse<Page<UserVerifyResponse>> list(HttpServletRequest request, CUserListRequest cUserListRequest){
        APIResponse<Page<UserVerifyResponse>> apiResponse = new APIResponse<>();
        Page<UserVerifyResponse> page = null;
        try {
            page = userVerifyHandler.getUserVerifyPage(cUserListRequest);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
            apiResponse.setData(page);
        } catch (Throwable e) {
            LOG.error("获取车主认证列表发生异常", cUserListRequest);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    /**
     * 审核状态更新
     * @param request
     * @param userVerify
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/audit",method = RequestMethod.POST)
    public APIResponse audit(HttpServletRequest request, UserVerify userVerify){
        APIResponse apiResponse = new APIResponse();
        try {
            userVerifyHandler.updateAuditStatus(userVerify);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
        } catch (Throwable e) {
            LOG.error("车主认证审核状态更新发生异常", userVerify);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

}
